import React, { Component } from 'react'
import {
  View,
  StyleSheet,
  Button,
  Platform
} from 'react-native'

import NavBar from '../../../../components/PublicHeader';
import Geolocation from '@react-native-community/geolocation';
import {
  PermissionsAndroid
} from 'react-native';
import {
  handleChange,
} from '../../../../utils/GetLocation';

export default class location extends Component {
  constructor(props) {
    super(props)
    this.state = {
      latitude: '',
      longitude: '',
      positions: ''
    }
  }

  componentDidMount = () => {
    if (Platform.OS === 'android') {
      this.requestLocationPermission()
    } else {
      Geolocation.requestAuthorization()
    }
  }

  // android获取地址权限
  async requestLocationPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          //第一次请求拒绝后提示用户你为什么要这个权限
          'title': '我要地址查询权限',
          'message': '没权限我不能工作，同意就好了'
        }
      )

      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      } else {
        this.requestLocationPermission()
      }
    } catch (err) {
      alert(err.toString())
    }
  }

  render() {
    return (
      <View style={styles.warper}>
        <NavBar
          navigation={this.props.navigation}
          title={"定位"}
        />
        <Button onPress={() => {
          handleChange(true)
        }} title={'定位'}></Button>
      </View >
    )
  }
}
const styles = StyleSheet.create({
  container: {
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'center',
    height: 32,
    backgroundColor: 'transparent'
  },
})